// pages/address/insert_address.js
let QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
let qqmapsdk;
import http from '../../utils/http.js'
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressId:null,
    consignee:'',
    tel:'',
    address:'',
    province:0,
    city:0,
    district:0,
    region:[]
  },

  bindRealName(e){
    this.setData({
      consignee:e.detail.value
    });
  },
  bindTel(e) {
    this.setData({
      tel: e.detail.value
    });
  },
  bindAddress(e) {
    this.setData({
      address: e.detail.value
    });
  },

  bindRegionChange(e) {
    let me = this;
    if (e.detail.value && e.detail.value.length == 3){
      console.log(e.detail.value);
      http.request({
        url: 'region/getRegionCodes/' + e.detail.value[0] + "/" + e.detail.value[1] + "/" + e.detail.value[2],
        success:res=>{
          if(!res.p || !res.c || !res.d){
            wx.showModal({
              title: '提示',
              content:"现只支持选择南京地区",
              showCancel:false
            })
            me.setData({
              region: []
            });
            return;
          }else{
            me.setData({
              province: res.p,
              city: res.c,
              district: res.d,
              region: e.detail.value
            });
          }
        }
      });
    }
  },

  getRemoteRegion(pname,cname,dname){
    let me=this;
    http.request({
      url: 'region/getRegionCodes/' + pname + "/" + cname + "/" + dname,
      success: res => {
        if (!res.p || !res.c || !res.d) {
          wx.showModal({
            title: '提示',
            content: "现只支持选择南京地区",
            showCancel: false
          })
          return;
        } else {
          me.setData({
            province: res.p,
            city: res.c,
            district: res.d,
            region: [pname,cname,dname]
          });
        }
      }
    });
  },

  saveAddress(){
    let me = this;
    if (!me.data.consignee) {
      wx.showToast({
        icon: 'none',
        title: '请填写收货人姓名'
      })
      return;
    }
    if (!me.data.tel) {
      wx.showToast({
        icon: 'none',
        title: '请填写收货人手机号'
      })
      return;
    }
    if (me.data.region.length==0) {
      wx.showToast({
        icon: 'none',
        title: '请选择收货人所在地区'
      })
      return;
    }
    if (!me.data.address) {
      wx.showToast({
        icon: 'none',
        title: '请填写收货人详细地址'
      })
      return;
    }
    let userId = wx.getStorageSync("userId");
    http.request({
      url:'user/saveAddress',
      loadTip: '正在请求',
      data:{
        consignee: me.data.consignee,
        tel: me.data.tel,
        userId: userId,
        province: me.data.province,
        city: me.data.city,
        district: me.data.district,
        address: me.data.address,
        addressId:me.data.addressId
      },
      success:res=>{
        wx.navigateBack()
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let me = this;
    qqmapsdk = new QQMapWX({
      key: 'XXQBZ-XUCE6-NACSM-MYORL-7SSDQ-QCBX4'
    });
    if(options.addressInfo){
      let address = JSON.parse(options.addressInfo);
      this.setData({
        addressId: address.addressId,
        consignee: address.consignee,
        tel: address.tel,
        address: address.address,
        province: address.province,
        city: address.city,
        district: address.district,
        region: [address.provinceName, address.cityName, address.districtName]
      });
    }else{
      wx.getLocation({
        success: function(res) {
          console.log(res);
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: res.latitude,
              longitude: res.longitude
            },
            success: function (res) {
              let addinfo = res.result.address_component;
              me.getRemoteRegion(addinfo.province, addinfo.city, addinfo.district);
            },
            fail: function (res) {
              wx.showToast({
                title: '定位失败，请检查是否已开启定位服务',
              })
            },
            complete: function (res) {
              console.log(res);
            }
          });
        },
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})